package com.quanyue.music.file.mapper;

import com.quanyue.music.domain.constant.FMChannel;
import com.quanyue.music.domain.constant.TimeZoneType;
import com.quanyue.music.domain.entity.ListenRecord;
import com.quanyue.music.file.type.ListenRecordResult;
import com.shzisg.mybatis.mapper.auto.AutoMapper;
import entity.UserRecord;
import org.apache.ibatis.annotations.*;

import java.time.LocalDateTime;
import java.util.List;

@Mapper
public interface ListenRecordMapper extends AutoMapper <ListenRecord> {

    @Select("select count(1) as user_count, r.channel, r.zone_type from qy_listen_record r "
            + "where r.del_flag = 0 "
            + "group by r.channel,r.zone_type")
    List <ListenRecordResult> findByChannel();

    @Delete("update qy_listen_record set del_flag = true "
            + "where TIMESTAMPDIFF(SECOND,modify_time,#{nowTime}) > 61 and del_flag = false")
    void truncateAll(@Param("nowTime") LocalDateTime nowTime);

    List <ListenRecord> findByDetail(@Param("fmId") String fmId, @Param("userId") String userId,
                                     @Param("channel") FMChannel channel, @Param("zoneType") TimeZoneType zoneType);

    @Update("update qy_listen_record set del_flag = true "
            + "where id NOT IN(#{id}) and user_id = #{userId}")
    int updateDelFlag(@Param("id") String id, @Param("userId") String userId);

    @Select("<script>select user_id,SUM(TIMESTAMPDIFF(SECOND,r.create_time,r.modify_time)) as count from qy_listen_record r "
            + "where r.channel = #{channel} and r.user_id in "
            + "<foreach collection=\"list\" item=\"id\" index=\"index\" open=\"(\" close=\")\" separator=\",\">#{id}</foreach> "
            + " group by user_id</script>")
    List<UserRecord> findLongByChannel(@Param("channel") FMChannel channel, @Param("list") List <String> userId);
}
